﻿namespace Pub.Class
{
    using System;
    using System.Collections.Specialized;
    using System.Configuration;
    using System.Web.Configuration;

    public class WebConfig
    {
        public static readonly string DoMain = (GetApp("DoMain") ?? string.Empty);

        public static NameValueCollection GetApp()
        {
            return ConfigurationManager.AppSettings;
        }

        public static string GetApp(int i)
        {
            if (ConfigurationManager.AppSettings[i].IsNotNull())
            {
                return ConfigurationManager.AppSettings[i].ToString();
            }
            return null;
        }

        public static string GetApp(string key)
        {
            if (ConfigurationManager.AppSettings[key].IsNotNull())
            {
                return ConfigurationManager.AppSettings[key].ToString();
            }
            return null;
        }

        public static ConnectionStringSettingsCollection GetConn()
        {
            return ConfigurationManager.ConnectionStrings;
        }

        public static string GetConn(int i)
        {
            if (ConfigurationManager.ConnectionStrings[i].IsNotNull())
            {
                return ConfigurationManager.ConnectionStrings[i].ToString();
            }
            return null;
        }

        public static string GetConn(string key)
        {
            if (ConfigurationManager.ConnectionStrings[key].IsNotNull())
            {
                return ConfigurationManager.ConnectionStrings[key].ToString();
            }
            return null;
        }

        public static TReturn GetSection<TReturn>(string sectionName)
        {
            return (TReturn) ConfigurationManager.GetSection(sectionName);
        }

        public static void RemoveApp(string key)
        {
            System.Configuration.Configuration configuration = WebConfigurationManager.OpenWebConfiguration("~");
            AppSettingsSection appSettings = configuration.AppSettings;
            if (appSettings.Settings[key].IsNotNull())
            {
                appSettings.Settings.Remove(key);
            }
            configuration.Save();
        }

        public static void SetApp(string key, string value)
        {
            System.Configuration.Configuration configuration = WebConfigurationManager.OpenWebConfiguration("~");
            AppSettingsSection section = (AppSettingsSection) configuration.GetSection("appSettings");
            if (section.Settings[key].IsNull())
            {
                section.Settings.Add(key, value);
            }
            else
            {
                section.Settings[key].Value = value;
            }
            configuration.Save();
        }

        public static void SetConn(string key, string connString, string providerName)
        {
            System.Configuration.Configuration configuration = WebConfigurationManager.OpenWebConfiguration("~");
            ConnectionStringsSection connectionStrings = configuration.ConnectionStrings;
            if (connectionStrings.ConnectionStrings[key].IsNull())
            {
                connectionStrings.ConnectionStrings.Add(new ConnectionStringSettings(key, connString, providerName));
            }
            else
            {
                connectionStrings.ConnectionStrings[key].ConnectionString = connString;
            }
            configuration.Save();
        }
    }
}

